var product_cart = new Vue({
    el: "#cart",
    data: {
        cartData: '',
        flag: false
    },
    mounted: function () {
        this.getCart();
    },
    methods: {
        getCart: function () {
            var cart = handleLocalStorage("get", "cart");
            if (!cart) {
                return false;
            } else {
                axios({
                    url: "/product/cart",
                    method: "post",
                    data: {
                        "cart": cart
                    },
                    headers: {
                        "Content-Type": "application/json"
                    }
                }).then(function (response) {
                    if (response.data.code == 200) {
                        product_cart.cartData = response.data.data;
                    } else {
                        window.location.href = "/view/other/404.html";
                    }
                }).catch(function (error) {
                    alert("服务器异常");
                });
            }
        },
        addNum: function (id, num) {
            var cart = handleLocalStorage("get", "cart");
            var flag = false;
            if (!cart) {
                var item = id + "-" + num + ",";
                handleLocalStorage("set", "cart", item);
            } else {
                var arr = cart.split(',');
                var arrItem;
                for (var i = 0; i < arr.length - 1; i++) {
                    arrItem = arr[i].split("-");
                    if (arrItem[0] == id) {
                        arrItem[1] = parseInt(arrItem[1]) + num;
                        arr[i] = arrItem[0] + "-" + arrItem[1];
                        handleLocalStorage("set", "cart", arr);
                        flag = true;
                    }
                }
                if (!flag) {
                    cart = cart + id + "-" + num + ",";
                    handleLocalStorage("set", "cart", cart);
                }
            }
            product_cart.getCart();
        },
        delNum: function (id) {
            var cart = handleLocalStorage("get", "cart");
            var arr = cart.split(',');
            var arrItem;
            for (var i = 0; i < arr.length - 1; i++) {
                arrItem = arr[i].split("-");
                if (arrItem[0] == id) {
                    if (parseInt(arrItem[1]) == 1) {
                        arr.splice(i, 1);
                    } else {
                        arrItem[1] = parseInt(arrItem[1]) - 1;
                        arr[i] = arrItem[0] + "-" + arrItem[1];
                    }
                }
            }
            handleLocalStorage("set", "cart", arr);
            product_cart.getCart();
        },
        delCartProduct: function (id) {
            var cart = handleLocalStorage("get", "cart");
            var arr = cart.split(',');
            for (var i = 0; i < arr.length - 1; i++) {
                arrItem = arr[i].split("-");
                if (arrItem[0] == id) {
                    arr.splice(i, 1);
                }
            }
            handleLocalStorage("set", "cart", arr);
            product_cart.getCart();
        }
    }
});
